/**
 * 
 */
package com.vision.admin.cl.ui.component;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.uibinder.client.UiConstructor;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.GridSelectionModel;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.BasicGridView;
import com.vision.core.cm.data.CompanyProperties;
import com.vision.core.cm.db.data.Company;

/**
 * @author Mark
 *
 */
public class CompanyGrid extends Grid<Company> {
	
	@UiConstructor
	public CompanyGrid() {
		super(Initializer.getInitStore(), Initializer.getInitColumns(), Initializer.getInitView());
	}
	
	public CompanyGrid(ListStore<Company> store, ColumnModel<Company> cm, GridView<Company> view) {
		super(store, cm, view);
		
		view.setAutoExpandColumn(getColumnModel().findColumnConfig(CompanyProperties.INSTANCE.name().getPath()));
	}
	
	static class Initializer {
		
		static GridSelectionModel<Company> selectionModel;
	
		public static GridSelectionModel<Company> getInitSelectionModel() {
//			if (selectionModel == null) {
//				IdentityValueProvider<Company> identity = new IdentityValueProvider<Company>();
//				selectionModel = new CheckBoxSelectionModel<Company>(identity);
//				selectionModel.setSelectionMode(SelectionMode.MULTI);
//			}
//			return selectionModel;
			return null;
		}
		
		@SuppressWarnings({ "rawtypes", "unchecked" })
		protected static ColumnModel<Company> getInitColumns() {
			CompanyProperties props = CompanyProperties.INSTANCE;
			
			List<ColumnConfig<Company, ?>> columns = new ArrayList<ColumnConfig<Company, ?>>();
	
			GridSelectionModel<Company> selectionModel = getInitSelectionModel();
			if (selectionModel instanceof CheckBoxSelectionModel)
				columns.add(((CheckBoxSelectionModel) selectionModel).getColumn());
			
			columns.add(UI.column(props.code(), 80, "Code"));
			columns.add(UI.column(props.name(), 200, "Name"));
	
			return new ColumnModel<Company>(columns);
		}
		
		protected static ListStore<Company> getInitStore() {
			return new ListStore<Company>(CompanyProperties.INSTANCE.key());
		}
		
		protected static GridView<Company> getInitView() {
			GridView<Company> view = new BasicGridView<Company>();
			view.setEmptyText("There are no companies to display.");
			return view;
		}
	
	}

}
